package com.rpoonolly.simplegame.mapio.jsoncoders;

import java.io.IOException;

import org.json.simple.JSONObject;

import com.rpoonolly.simplegame.core.aabb.AABBEntity;

public class AABBEntityJSONCoder extends JSONCoder<AABBEntity> {
	private static AABBEntityJSONCoder singletonInstance;

	private AABBEntityJSONCoder() {}

	public static AABBEntityJSONCoder getInstance() {
		if (singletonInstance == null) {
			singletonInstance = new AABBEntityJSONCoder();
		}

		return singletonInstance;
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject encode(AABBEntity obj) throws IOException {
		JSONObject jsonEnt = new JSONObject();
		jsonEnt.put("x", obj.getX());
		jsonEnt.put("y", obj.getY());
		jsonEnt.put("width", obj.getWidth());
		jsonEnt.put("height", obj.getHeight());
		
		return jsonEnt;
	}

	@Override
	public AABBEntity decode(JSONObject jsonEnt) throws IOException {
		int x = (Integer) jsonEnt.get("x");
		int y = (Integer) jsonEnt.get("y");
		int width = (Integer) jsonEnt.get("width");
		int height = (Integer) jsonEnt.get("height");
		
		return new AABBEntity(x, y, width, height);
	}
}
